package com.huatai.exchange.bootup.controller;

import com.faneui.bootup.controller.AbstractRestController;
import com.faneui.common.bean.FaneuiBaseResp;
import com.faneui.common.bean.IdReq;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.BeanMapper;
import com.faneui.common.utils.ListUtils;
import com.google.common.collect.Lists;
import com.huatai.exchange.dal.model.ExcExchangeDataFile;
import com.huatai.exchange.dal.model.ExcExchangeInfo;
import com.huatai.exchange.facade.ExchangeInfoController;
import com.huatai.exchange.facade.bean.ExchangeInfoVo;
import com.huatai.exchange.facade.bean.ModifyExchangeInfoDataFileListReq;
import com.huatai.exchange.facade.bean.QueryExchangeInfoVoListResp;
import com.huatai.exchange.service.ExcExchangeDataFileService;
import com.huatai.exchange.service.ExcExchangeInfoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件数据交换中心
 */
@RestController
@RequestMapping(value = "exchange")
@Api("数据文件交换配置信息")
public class ExchangeInfoControllerImpl extends AbstractRestController implements ExchangeInfoController {

    @Autowired
    private ExcExchangeInfoService exchangeInfoService;

    @Autowired
    private ExcExchangeDataFileService excExchangeDataFileService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public QueryExchangeInfoVoListResp queryExchangeInfoVoList(@ModelAttribute ExchangeInfoVo exchangeInfoVo) throws BusinessException {
        QueryExchangeInfoVoListResp resp = new QueryExchangeInfoVoListResp();
        try {
            ExcExchangeInfo param = BeanMapper.map(exchangeInfoVo, ExcExchangeInfo.class);
            List<ExcExchangeInfo> infos = exchangeInfoService.queryExcExchangeInfoList(param);
            if (ListUtils.isNotEmpty(infos)) {
                List<ExchangeInfoVo> vos = Lists.newArrayList();
                for (ExcExchangeInfo info : infos) {
                    List<ExcExchangeDataFile> list = excExchangeDataFileService.queryExcExchangeDateFileList(info.getId());
                    ExchangeInfoVo vo = BeanMapper.map(info, ExchangeInfoVo.class);
                    vo.setFileNum(ListUtils.isEmpty(list) ? 0 : list.size());
                    vos.add(vo);
                }
                resp.setExchangeInfoVos(vos);
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @Override
    public FaneuiBaseResp modifyExchangeInfoDataFileList(@ModelAttribute ModifyExchangeInfoDataFileListReq req) throws Exception {
        FaneuiBaseResp resp = new FaneuiBaseResp();
        try{

        }catch (Exception ex) {

        }
        return null;
    }
}
